package io.lkt.modules.sys.wechat.service;

import com.tulele.vtool.model.PlatformSysInfo;
import io.lkt.modules.sys.service.SysGzhService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Scope("prototype")//多例注入
@Service
@Slf4j
public class WeixinServiceDL extends WxMpServiceImpl {
    @Autowired
    private SysGzhService sysGzhService;
    
    public void reSetConfigStorage(String gzhCode) {

        PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(gzhCode);

        if(sysGzh!=null) {

            final WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();

            // 设置微信公众号的appid
            config.setAppId(sysGzh.getSysGzh().getAppId());
            // 设置微信公众号的app corpSecret
            config.setSecret(sysGzh.getSysGzh().getSecret());
            // 设置微信公众号的token
            config.setToken(sysGzh.getSysGzh().getToken());
            // 设置消息加解密密钥
            config.setAesKey(sysGzh.getSysGzh().getAesKey());

            //log.error("******** reSetConfigStorage_config.getAppId():"+config.getAppId()+"_*****");
            super.setWxMpConfigStorage(config);
            //this.refreshRouter();
        }
    }

    private void refreshRouter() {
        /*final WxMpMessageRouter newRouter = new WxMpMessageRouter(this);

        // 记录所有事件的日志
        newRouter.rule().handler(this.logHandler).next();

        // 自定义菜单事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.CLICK).handler(this.menuHandler).end();

        // 点击菜单连接事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.VIEW).handler(this.menuHandler).end();

        // 关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE).handler(this.subscribeHandler)
                .end();

        // 取消关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.UNSUBSCRIBE).handler(this.unsubscribeHandler)
                .end();

        // 默认
        newRouter.rule().async(false).handler(this.msgHandler).end();

        //log.error("******** refreshRouter_newRouter:"+newRouter.toString()+"_*****");

        this.router = newRouter;*/
    }
}
